package com.wnhz.work.jy.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.solidfire.gson.Gson;
import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.OrderAdapter;
import com.wnhz.work.jy.adapter.OrderDetailsAdapter;
import com.wnhz.work.jy.bean.OrderDetailsResult;
import com.wnhz.work.jy.bean.OrderShoppingBean;
import com.wnhz.work.jy.bean.ShoppingBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ----订单详情
 */
public class OrderDetailsActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;

    private ListView listView;
    private OrderDetailsAdapter mAdapter;

    private List<OrderShoppingBean> orderList = new ArrayList<>();

    private TextView tv_order_code;
    private TextView tv_time;
    private TextView tv_name;
    private TextView tv_phone;
    private TextView tv_address;
    private TextView tv_coupon;
    private TextView tv_total_price;
    private TextView tv_freight;
    private TextView tv_favorable;
    private TextView tv_total;
    private TextView tv_start;

    private String orderId;

    private OrderDetailsResult orderDetailsResult;
    private View address_lay;

    private View scrollView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details);
        orderId = getIntent().getStringExtra("orderId");
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void initView() {
        scrollView = findViewById(R.id.scrollView);
        address_lay = findViewById(R.id.address_lay);
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("订单详情");
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        tv_order_code = (TextView) findViewById(R.id.tv_order_code);//订单编号
        tv_start = (TextView) findViewById(R.id.tv_start);//订单状态
        tv_time = (TextView) findViewById(R.id.tv_time);//创建时间
        tv_name = (TextView) findViewById(R.id.tv_name);//名字
        tv_phone = (TextView) findViewById(R.id.tv_phone);//手机号
        tv_address = (TextView) findViewById(R.id.tv_address);//地址
        tv_coupon = (TextView) findViewById(R.id.tv_coupon);//优惠券使用情况
        tv_total_price = (TextView) findViewById(R.id.tv_total_price);//商品总价
        tv_freight = (TextView) findViewById(R.id.tv_freight);//运费
        tv_favorable = (TextView) findViewById(R.id.tv_favorable);//优惠金额
        tv_total = (TextView) findViewById(R.id.tv_total);//实际总价
        addListView();
    }

    private void setData(){
        scrollView.setVisibility(View.VISIBLE);
        tv_order_code.setText(orderDetailsResult.getOrder_no());
        tv_time.setText(orderDetailsResult.getAddtime());
        tv_total_price.setText("¥"+orderDetailsResult.getPay_fee());
        tv_total.setText("¥"+orderDetailsResult.getPay_fee());
        int max = orderDetailsResult.getInfo().size();
        String start = orderDetailsResult.getState();

        if (start != null && !"".equals(start)){
            switch (Integer.parseInt(orderDetailsResult.getState())){
                case 0:
                    tv_start.setText("待付款");
                    break;
                case 1:
                    tv_start.setText("待发货");
                    break;
                case 2:
                    tv_start.setText("待收货");
                    break;
                case 3:
                    tv_start.setText("待评价");
                    break;
                case 4:
                    tv_start.setText("已完成");
                    break;
                case 5:
                    tv_start.setText("已取消");
                    break;
                case 6:
                    tv_start.setText("退款");
                    break;
            }
            for (int i = 0; i < max; i++) {
                orderDetailsResult.getInfo().get(i).setState(start);
            }
        }

        if (orderDetailsResult.getPay_fee() != null && !"".equals(orderDetailsResult.getPay_fee())
                && orderDetailsResult.getAllprice() != null && !"".equals(orderDetailsResult.getAllprice())){
            double d = Double.parseDouble(orderDetailsResult.getAllprice())-Double.parseDouble(orderDetailsResult.getPay_fee());
            DecimalFormat df = new DecimalFormat("##.#");
            tv_favorable.setText("¥"+df.format(d));
        }
        if ("1".equals(orderDetailsResult.getDelivery())){
            address_lay.setVisibility(View.VISIBLE);
            String address = orderDetailsResult.getProvince()+orderDetailsResult.getCity()+orderDetailsResult.getCountry()+orderDetailsResult.getAddress();
            tv_address.setText(address);
            tv_name.setText(orderDetailsResult.getTruename());
            tv_phone.setText(orderDetailsResult.getTelephone());
        }else {
            address_lay.setVisibility(View.GONE);
        }
    }

    private void addListView() {
        //final View view = getLayoutInflater().inflate(R.layout.addorderlistview, null);
        listView = (ListView) findViewById(R.id.view_add_listview);

        mAdapter = new OrderDetailsAdapter(OrderDetailsActivity.this, orderList, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int index = (int) v.getTag();
                if (!"1".equals(orderList.get(index).getIs_evaluation())){
                    startActivity(new Intent(OrderDetailsActivity.this, EvaluateActivity.class)
                            .putExtra("goodsid",orderList.get(index).getGoodid())
                            .putExtra("order_detail_id",orderList.get(index).getOrderid()));
                }

            }
        });
        mAdapter.setType(1);
        listView.setAdapter(mAdapter);
    }


    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            default:
                break;
        }
    }

    private static final String TAG = "OrderDetailsActivity";
    private void getData(){
        Map<String,Object> map = new HashMap<>();
        map.put("id",orderId);
        showDialog();
        XUtil.Post(ConfigUtil.ORDERINFO,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: OrderDetailsActivity"+result );
                orderList.clear();
                orderDetailsResult = new Gson().fromJson(result,OrderDetailsResult.class);
                setData();
                orderList.addAll(orderDetailsResult.getInfo());
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }
}
